package com.wn.class30.service.impl;

import com.wn.class30.entity.MyorderShare;
import com.wn.class30.entity.MyorderShareExample;
import com.wn.class30.mapper.MyorderShareDao;
import com.wn.class30.service.MyorderShareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yuyu
 * @since 2020/4/18 9:49
 */
@Service
public class MyorderShareServiceImpl implements MyorderShareService {

    @Resource
    private MyorderShareDao myorderShareDao;
    @Override
    public long count(Map map) throws Exception{

        return myorderShareDao.countByExample(getMyorderShareExample(map));
    }

    @Override
    public List<MyorderShare> selectMyorderShares(Map<String, String> map) throws Exception{

        return myorderShareDao.selectByExample(getMyorderShareExample(map));
    }

    @Override
    public int updateByPrimaryKeySelective(MyorderShare myorderShare) {
        return 0;
    }

    @Override
    public long selectCount(Integer abusinessId) {
        MyorderShareExample myorderShareExample = new MyorderShareExample();
        MyorderShareExample.Criteria criteria = myorderShareExample.createCriteria();
        criteria.andBusinessIdEqualTo(abusinessId);
        return myorderShareDao.countByExample(myorderShareExample);
    }


    public MyorderShareExample getMyorderShareExample(Map<String,String> map) throws ParseException {
        MyorderShareExample myorderShareExample = new MyorderShareExample();
        MyorderShareExample.Criteria criteria = myorderShareExample.createCriteria();
        Long page = Long.parseLong(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        String businessName = map.get("businessName");
        String businessId= map.get("businessId");
        String isSettlement= map.get("isSettlement");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        if(!"".equals(businessName) && businessName != null){
                criteria.andBusinessNameLike("%"+businessName+"%");

        }
        if(!"".equals(businessId) && businessId != null){

                criteria.andBusinessIdEqualTo(Integer.parseInt(businessId));

        }
        if(!"".equals(startDate) && startDate != null){

            criteria.andMyorderShareTimeGreaterThanOrEqualTo(simpleDateFormat.parse(startDate));
        }

        if(!"".equals(endDate) && endDate != null){
            Date date = simpleDateFormat.parse(endDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time = c.getTime();
            String format = simpleDateFormat.format(time);
            criteria.andMyorderShareTimeLessThanOrEqualTo(simpleDateFormat.parse(format));
        }
        if(!"".equals(isSettlement) && isSettlement != null){
                criteria.andMyorderShareStatusEqualTo(Integer.parseInt(isSettlement));
        }
        myorderShareExample.setLimit(limit);
        myorderShareExample.setOffset(page);
        myorderShareExample.setOrderByClause("`myorder_share_status` ASC");
        return myorderShareExample;
    }
}
